package com.baomidou.ant.service.impl;

import com.baomidou.ant.entity.*;
import com.baomidou.ant.mapper.MachineInfoMapper;
import com.baomidou.ant.service.*;
import com.baomidou.ant.vo.request.RequestAddMachineInfoVo;
import com.baomidou.ant.vo.request.RequestListMachineInfoVo;
import com.baomidou.ant.vo.response.ResponseListMachineInfoVo;
import com.baomidou.ant.vo.response.ResponseMachineNameVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2022-03-18
 */
@Service
public class MachineInfoServiceImpl extends ServiceImpl<MachineInfoMapper, MachineInfo> implements IMachineInfoService {
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private IAllMachineService allMachineService;
    @Autowired
    private IProcessInfoService processInfoService;

    @Override
    public MachineInfo queryByModel(RequestAddMachineInfoVo vo) {
        QueryWrapper<MachineInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("machine_model",vo.getMachineModel());
        queryWrapper.eq("dept_id",vo.getDeptId());
        List<MachineInfo> machineList=list(queryWrapper);
        if(machineList==null||machineList.size()==0){
            return null;
        }else
            return machineList.get(machineList.size()-1);
    }

    @Override
    public List<ResponseMachineNameVo> listMachineName(Integer deptId) {
        QueryWrapper<MachineInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("dept_id",deptId);
        List<MachineInfo> machineInfoList=list(queryWrapper);
        List<ResponseMachineNameVo> list=new ArrayList<>();
        for(MachineInfo machineInfo:machineInfoList){
            ResponseMachineNameVo vo=new ResponseMachineNameVo();
            BeanUtils.copyProperties(machineInfo,vo);
            list.add(vo);
        }
        return list;
    }

    @Override
    public IPage<ResponseListMachineInfoVo> listMachine(RequestListMachineInfoVo requestListMachineInfoVo) {
        IPage<MachineInfo> iPage=new Page<>(requestListMachineInfoVo.getDisplayStart(),requestListMachineInfoVo.getDisplayLength());
        QueryWrapper<MachineInfo> queryWrapper=new QueryWrapper<>();
        if(StringUtils.isNotBlank(requestListMachineInfoVo.getMachineModel())){
            queryWrapper.eq("machine_model",requestListMachineInfoVo.getMachineModel());
        }
        SysUser user=userService.getById(requestListMachineInfoVo.getUserId());
        //判断管理员级别
        if(Objects.equals(user.getType(), "1")){
            //管理员条件查询
            if(StringUtils.isNotBlank(requestListMachineInfoVo.getDeptName())){
                Integer deptId=deptService.queryByDeptName(requestListMachineInfoVo.getDeptName());
                queryWrapper.eq("dept_id",deptId);
            }
        }else if(Objects.equals(user.getType(), "0")||Objects.equals(user.getType(), "2")){//二级管理员,只能看自己部门的
            queryWrapper.eq("dept_id",user.getDeptId());
        }
        IPage<MachineInfo> page=page(iPage,queryWrapper);
        return page.convert(machineInfo -> {
            ResponseListMachineInfoVo vo=new ResponseListMachineInfoVo();
            BeanUtils.copyProperties(machineInfo,vo);
            vo.setDeptName(deptService.getById(machineInfo.getDeptId()).getDept());
            QueryWrapper<AllMachine> allMachineQueryWrapper=new QueryWrapper<>();
            allMachineQueryWrapper.eq("machine_info_id",machineInfo.getId());
            vo.setMachineAmount(allMachineService.list(allMachineQueryWrapper).size()+"台");
            //获取作用工序
            QueryWrapper<ProcessInfo> processInfoQueryWrapper=new QueryWrapper<>();
            processInfoQueryWrapper.eq("process_dept",machineInfo.getDeptId());
            List<ProcessInfo> processInfoList=processInfoService.list(processInfoQueryWrapper);
            StringBuilder processName=new StringBuilder();
            for(ProcessInfo processInfo:processInfoList){
                List<String> machineIds= Arrays.asList(processInfo.getMachine().split(","));
                if(machineIds.contains(String.valueOf(machineInfo.getId()))){
                    processName.append(processInfo.getProcessName()).append(";");
                }
            }
            //
            if(ObjectUtils.isEmpty(processName)){
                vo.setProcessName("暂无(待分配)");
            }else
                vo.setProcessName(processName.toString());
            return vo;
        });
    }

    @Override
    public boolean addMachine(RequestAddMachineInfoVo vo) {
        MachineInfo machineInfo=new MachineInfo();
        machineInfo.setDeptId(vo.getDeptId());
        machineInfo.setMachineModel(vo.getMachineModel());
        return save(machineInfo);
    }
}
